#ifndef DLIB_PHPSERIALIZE_H
#define DLIB_PHPSERIALIZE_H

//#ifdef __cplusplus
//extern "C" {
//#endif

typedef int     dlib_php_bool_t;
typedef int     dlib_php_int_t;
typedef double  dlib_php_double_t;
typedef char*   dlib_php_string_t;
typedef int     dlib_php_null_t;

typedef struct _dlib_php_array {
    int count;
    struct _dlib_php_element *elements;
} dlib_php_array_t;

typedef struct _dlib_php_object {
    int count;
    struct _dlib_php_element *members;
    char *name;
} dlib_php_object_t;

typedef enum _dlib_php_data_type {
    DLIB_PHP_DATATYPE_UNKNOWN = 0,
    DLIB_PHP_DATATYPE_BOOL,
    DLIB_PHP_DATATYPE_INT,
    DLIB_PHP_DATATYPE_DOUBLE,
    DLIB_PHP_DATATYPE_STRING,
    DLIB_PHP_DATATYPE_NULL,
    DLIB_PHP_DATATYPE_ARRAY,
    DLIB_PHP_DATATYPE_OBJECT,
} dlib_php_data_type_t;

typedef struct _dlib_php_element {
    union {
        dlib_php_bool_t b;
        dlib_php_int_t i;
        dlib_php_double_t d;
        dlib_php_string_t s;
        dlib_php_null_t n;
        dlib_php_array_t a;
        dlib_php_object_t o;
    };
    dlib_php_data_type_t t;
} dlib_php_element_t;

typedef dlib_php_element_t* dlib_php_handle;

#define DLIB_PHP_INVALID_HANDLE 0

dlib_php_handle dlib_php_unserialize(char *buf, int len);
int dlib_php_serialize(dlib_php_handle handle, char *buf, int *len);
void dlib_php_release_handle(dlib_php_handle handle);

dlib_php_handle dlib_php_create_bool(int b);
dlib_php_handle dlib_php_create_integer(int i);
dlib_php_handle dlib_php_create_double(double d);
dlib_php_handle dlib_php_create_string(char *s, int len);
dlib_php_handle dlib_php_create_null(void);
dlib_php_handle dlib_php_create_array(int count);
dlib_php_handle dlib_php_create_object(char *name, int len, int count);

int dlib_php_set_bool(dlib_php_handle handle, int b);
int dlib_php_set_integer(dlib_php_handle handle, int i);
int dlib_php_set_double(dlib_php_handle handle, double d);
int dlib_php_set_string(dlib_php_handle handle, char *buf, int len);
int dlib_php_set_null(dlib_php_handle handle);
int dlib_php_set_array(dlib_php_handle handle, int count);
int dlib_php_set_array_element_bool(dlib_php_handle handle, int idx, char *key, int b);
int dlib_php_set_array_element_integer(dlib_php_handle handle, int idx, char *key, int i);
int dlib_php_set_array_element_double(dlib_php_handle handle, int idx, char *key, double d);
int dlib_php_set_array_element_string(dlib_php_handle handle, int idx, char *key, char *s, int len);
int dlib_php_set_array_element_null(dlib_php_handle handle, int idx, char *key);
int dlib_php_set_array_element_array(dlib_php_handle handle, int idx, char *key, int count);
int dlib_php_set_array_element_object(dlib_php_handle handle, int idx, char *key, char *name, int len, int count);
int dlib_php_set_object(dlib_php_handle handle, char *name, int len, int count);
int dlib_php_set_object_member_bool(dlib_php_handle handle, int idx, char *key, int b);
int dlib_php_set_object_member_integer(dlib_php_handle handle, int idx, char *key, int i);
int dlib_php_set_object_member_double(dlib_php_handle handle, int idx, char *key, double d);
int dlib_php_set_object_member_string(dlib_php_handle handle, int idx, char *key, char *s, int len);
int dlib_php_set_object_member_null(dlib_php_handle handle, int idx, char *key);
int dlib_php_set_object_member_array(dlib_php_handle handle, int idx, char *key, int count);
int dlib_php_set_object_member_object(dlib_php_handle handle, int idx, char *key, char *name, int len, int count);

dlib_php_element_t * dlib_php_get_element(dlib_php_handle handle);
int dlib_php_get_bool(dlib_php_handle handle);
int dlib_php_get_integer(dlib_php_handle handle);
double dlib_php_get_double(dlib_php_handle handle);
char * dlib_php_get_string(dlib_php_handle handle);
int dlib_php_get_null(dlib_php_handle handle);
int dlib_php_get_array_count(dlib_php_handle handle);
dlib_php_element_t * dlib_php_get_array_element_by_pos(dlib_php_handle handle, int idx);
dlib_php_element_t * dlib_php_get_array_element_by_key(dlib_php_handle handle, char *key);
char * dlib_php_get_object_name(dlib_php_handle handle);
int dlib_php_get_object_count(dlib_php_handle handle);
dlib_php_element_t * dlib_php_get_object_member(dlib_php_handle handle, char *name);

//#ifdef __cplusplus
//}
//#endif

#endif//DLIB_PHPSERIALIZE_H
